# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json


# useful for handling different item types with a single interface


import json


class LocalbondPipeline:
    def open_spider(self, spider):
        print("开启管道")
        self.items = []

    def process_item(self, item, spider):
        item_dict = dict(item)
        self.items.append(item_dict)
        return item

    def close_spider(self, spider):
        print("关闭管道")
        try:
            with open("localbond.json", "w", encoding="utf-8") as fp:
                json.dump(self.items, fp, ensure_ascii=False, indent=4)
        except Exception as e:
            print(f"写入文件时出现错误: {e}")
